package com.dabaozhan.common.utils.aliyun.ocr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.http.HttpResponse;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.Base64Utils;
import com.dabaozhan.common.utils.aliyun.api.util.HttpUtils;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author houzhong
 * @date 2019-01-09 10:53
 */
public class IndustryUtils {

    public static JSONObject getIndustry(String file){

        JSONObject dataValueObj = null;

        //String img = "/Users/houzhong/Downloads/WechatIMG14.jpeg";
        String base64 = Base64Utils.base64(file);

        String host = "https://dm-58.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_business_license.json";
        String method = "POST";
        String appcode = "415ad20123d2416d918f86cfdde1ffbb";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = "{\"image\":\""+base64+"\"}";

        try {
            org.apache.http.HttpResponse response = HttpUtils.doPost(host, path, method, headers,querys, bodys);
            //System.out.println(response.toString());
            //获取response的body
            dataValueObj = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            System.out.println(dataValueObj);
        } catch (RRException e) {
            throw new RRException(ResponseCodeEnum.ResponseCode_301.getCode(), e.getMessage());
        } catch (Exception e) {
            //throw new RRException(ResponseCodeEnum.ResponseCode_301.getCode(), e.getMessage());
        }

        return dataValueObj;

    }

    public static void main(String[] args) {

        String img = "/Users/houzhong/Downloads/WechatIMG11.jpeg";
        JSONObject object = IndustryUtils.getIndustry(img);

        System.out.println(object);

        /*String a = "{\"capital\":\"人民币200.0000万元整\",\"address\":\"上海市崇明区堡镇堡镇南路58号34幢2楼206-5室(上海堡镇经济小区)\",\"reg_num\":\"91310230MA1K19947L\",\"valid_period\":\"29991231\",\"qrcode\":{\"top\":1549,\"left\":273,\"width\":239,\"height\":251},\"stamp\":{\"top\":1773,\"left\":1152,\"width\":321,\"height\":326},\"title\":{\"top\":624,\"left\":451,\"width\":852,\"height\":177},\"type\":\"有限责任公司(自然人投资或控股)\",\"person\":\"罗星宇\",\"success\":true,\"name\":\"上海宇穹信息科技有限公司\",\"angle\":0,\"establish_date\":\"20180713\",\"captial\":\"人民币200.0000万元整\",\"emblem\":{\"top\":237,\"left\":735,\"width\":285,\"height\":306},\"request_id\":\"20190114164515_92817553b49cfb81f598d02b67da7613\"}";

        JSONObject object = JSONObject.parseObject(a);

        //统一社会信用代码
        String unifiedCode = object.getString("reg_num");
        //注册资金
        String registeredCapital = object.getString("capital");
        //注册地址
        String residence = object.getString("address");
        //法人
        String name = object.getString("person");

        System.out.println(name);*/


       /* String a = "{\"code\": \"1\",\"msg\": \"识别成功\",\"result\": {\"法定代表人\": {\"words\": \"严利秋\"},\"营业期限\": {\"words\": \"2017年08月07日至长期\"}}}";
        JSONObject object = JSONObject.parseObject(a);
        JSONObject re = object.getJSONObject("result");

        String code = object.getString("code");
        if ("1".equals(code)) {
            System.out.println(code);
        }



        String name = re.getJSONObject("法定代表人").getString("words");

        String name11 = re.getJSONObject("营业期限").getString("words");
        System.out.println(name);
        System.out.println(name11);*/
        /*String img = "/Users/houzhong/Downloads/WechatIMG14.jpeg";
        JSONObject dataValueObj = getIndustry(img);

        JSONObject out = dataValueObj.getJSONObject("result");

        String name = out.getString("法定代表人");*/

//
//        JSONObject dataValueObj = null;
//
//        String img = "/Users/houzhong/Downloads/WechatIMG14.jpeg";
//
//        String base64 = Base64Utils.base64(img);
//
//        String host = "https://bnesslic.market.alicloudapi.com";
//        String path = "/businesslicense";
//        String method = "POST";
//        String appcode = "415ad20123d2416d918f86cfdde1ffbb";
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        //根据API的要求，定义相对应的Content-Type
//        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        Map<String, String> querys = new HashMap<String, String>();
//        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("image", base64);
//
//        try {
//            org.apache.http.HttpResponse response = HttpUtils.doPost(host, path, method, headers,querys, bodys);
//            //System.out.println(response.toString());
//            //获取response的body
//            dataValueObj = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
//            System.out.println(dataValueObj);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
